﻿/*
Copyright (c) 2011 <a href="http://www.gutgames.com">James Craig</a>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.*/

#region Usings
using System;
using System.Collections.Generic;
using System.Timers;
using BlammoNet.Interfaces;
using Utilities.DataTypes.Patterns.BaseClasses;
#endregion

namespace BlammoNet
{
    /// <summary>
    /// Logging Manager
    /// </summary>
    public class Manager : Singleton<Manager>, IDisposable
    {
        #region Constructor

        /// <summary>
        /// Constructor
        /// </summary>
        protected Manager()
            : base()
        {
            Logs = new Dictionary<string, ILog>();
            Manager.Configuration = Gestalt.Manager.Instance.GetConfigFile<Config.Config>("LogFile");
            if (Manager.Configuration.Interval > 0)
            {
                FileTimer = new Timer(Configuration.Interval);
                FileTimer.Elapsed += new ElapsedEventHandler(FileTimer_Elapsed);
                FileTimer.Start();
            }
        }

        #endregion

        #region Events

        /// <summary>
        /// Called when the timer is up and we must switch to a new log file
        /// </summary>
        /// <param name="sender">Sender object</param>
        /// <param name="e">Elapsed event arguments</param>
        void FileTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            foreach (string Key in Logs.Keys)
            {
                Logs[Key].TimeElapsed();
            }
        }

        #endregion

        #region Public Functions

        /// <summary>
        /// Gets a specified log
        /// </summary>
        /// <param name="Name">The name of the log file</param>
        /// <returns>The log file specified</returns>
        public ILog GetLog(string Name = "Default")
        {
            if (!Logs.ContainsKey(Name))
                Logs.Add(Name, new FileLog(Name));
            return Logs[Name];
        }

        /// <summary>
        /// Adds a log object or replaces one already in use
        /// </summary>
        /// <param name="Log">Log file to add</param>
        /// <param name="Name">The name of the log file</param>
        public void AddLog(ILog Log, string Name = "Default")
        {
            if (Logs.ContainsKey(Name))
                Logs[Name] = Log;
            else
                Logs.Add(Name, Log);
        }

        #endregion

        #region Private and Internal Properties

        private Timer FileTimer { get; set; }

        private static Dictionary<string, ILog> Logs { get; set; }
        internal static Config.Config Configuration { get; set; }

        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            if (FileTimer != null)
            {
                FileTimer.Dispose();
                FileTimer = null;
            }
            if (Logs != null)
            {
                foreach (string Key in Logs.Keys)
                {
                    Logs[Key].Dispose();
                }
            }
        }

        #endregion
    }
}